# 搜索
from flask import request
from flask_restful import Resource, fields, marshal

from app.models import Poem


class Search(Resource):
    def get(self):
        name = request.args.get('name')
        ani_into = {
            'p_id': fields.Integer(attribute='p_id'),
            'p_name': fields.String(attribute='p_name'),
            # 'a_name': fields.String(attribute='author'),
            # 'd_name': fields.String(attribute='dynasty'),
            'p_content': fields.String(attribute='p_content'),
            'p_analyze': fields.String(attribute='p_analyze'),
            'p_annotation': fields.String(attribute='p_annotation'),
            'p_view': fields.Integer(attribute='p_view'),
            'p_collect_count': fields.Integer(attribute='p_collect_count'),
        }
        pome = Poem.query.filter(Poem.p_name.like('%{}%'.format(name))).all()  # 模糊获取匹配的所有诗歌
        # if pome.count() == 0:
        #     return {'msg': 'please register', 'status': 404}
        return_value = {
            'returnCode': fields.Integer,
            'returnValue': fields.Nested(ani_into)
        }
        return marshal({'returnCode': 200, 'returnValue': pome}, return_value)
